class Subject { // 被观察者
    constructor(name) {
        this.name = name;
        this.state = 'happy';
        this.observers = [];
    }

    attach(observer) { // 被观察者需要收集观察者
        this.observers.push(observer);
    }

    setState(newState) {
        this.observers.forEach(observer => {
            observer.update({name: this.name, newState})
        })
    }
}

class  Observer { // 观察者
    constructor(name) {
        this.name = name;
    }

    update(data) {
        console.log(data);
    }
}

let s1 = new Subject('小宝宝');

let o1 = new Observer('爸爸');
let o2 = new Observer('妈妈');

s1.attach(o1);
s1.attach(o2);

s1.setState('hungry')